<template>
  <div>
    <div class="header">
      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="collapse = !collapse">
        <i v-if="!collapse" class="el-icon-s-fold"></i>
        <i v-else class="el-icon-s-unfold"></i>
      </div>
      <div class="logo">互联网医院医院管理端</div>
      <div class="header-right">
        <div class="header-user-con">
          <!-- 用户头像 -->
          <div class="user-avator">
            <img src="@/assets/img/img.jpg" />
          </div>
          <!-- 下拉菜单 -->
          <el-dropdown class="user-name" @command="handleCommand">
            <span class="el-dropdown-link">
              {{username}}
              <i class="el-icon-caret-bottom"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="changepwd">修改密码</el-dropdown-item>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <div class="sidebar">
      <!--background-color="#324157" text-color="#bfcbd9" active-text-color="#20a0ff"-->
      <el-menu :default-active="getActive" class="sidebar-el-menu" :collapse="collapse" unique-opened router>
        <div v-for="(menu, index) in menus" :key="index">
          <div v-if="menu.children && menu.children.length > 0">
            <el-submenu :index="menu.url">
              <template slot="title">
                <i :class="menu.icon"></i>
                <span slot="title">{{ menu.name }}</span>
              </template>
              <div v-for="(subMenu, index2) in menu.children" :key="index2">
                <!-- 
                <el-submenu v-if="subMenu.children && subMenu.children.length > 0" :index="subMenu.url">
                  <template slot="title">{{ subMenu.name }}</template>
                  <el-menu-item v-for="(threeMenu, index3) of subMenu.children" :key="index3" :index="threeMenu.url">
                    <i :class="threeMenu.icon"></i>
                    <span slot="title">{{ threeMenu.name }}</span>
                  </el-menu-item>
                </el-submenu>
                <el-menu-item v-else :index="subMenu.url">{{ subMenu.name }}</el-menu-item>
                -->
                <el-menu-item :index="subMenu.url">{{ subMenu.name }}</el-menu-item>
              </div>
            </el-submenu>
          </div>
          <div v-else>
            <el-menu-item :index="menu.url">
              <i :class="menu.icon"></i>
              <span slot="title">{{ menu.name }}</span>
            </el-menu-item>
          </div>
        </div>
      </el-menu>
    </div>
    <div class="content-box" :class="{'content-collapse':collapse}">
      <v-tags></v-tags>
      <div class="content">
        <transition name="move" mode="out-in">
          <keep-alive :include="tagsList">
            <router-view></router-view>
          </keep-alive>
        </transition>
        <el-backtop target=".content"></el-backtop>
      </div>
    </div>
    <!-- 修改密码窗体 -->
    <el-dialog title="修改密码" :visible.sync="dialogVisible" :before-close="closeDialog">
      <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="80px">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="dataForm.password" placeholder="新密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input v-model="dataForm.checkPassword" placeholder="确认密码" show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import jsonNav from '@/assets/json/nav.json';
import {dataToTree} from '@/utils';
import vTags from '@/components/Tags';
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.dataForm.checkPassword !== '') {
          this.$refs.dataForm.validateField('checkPassword');
        }
        callback();
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.dataForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    }
    return {
      tagsList: [],
      collapse: false,
      menus: [],
      dialogVisible: false,
      dataForm: {
        id: '',
        password: '',
        checkPassword: ''
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPassword: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    vTags
  },
  mounted () {
    this.getNav();
  },
  computed: {
    getActive () {
      return this.$route.path.replace('/', '');
    },
    username () {
      let loginUser = JSON.parse(sessionStorage.getItem('loginUser'));
      return loginUser.username || 'ADMIN';
    }
  },
  methods: {
    getNav() {
      this.menus = jsonNav;
      // this.$ajax.get('/permission/nav').then(res => {
      //   if(res.code == 0){
      //     this.menus = dataToTree(res.data);
      //     console.log(JSON.stringify(this.menus))
      //   }
      // }).catch(error => console.log(error));
    },
    // 用户名下拉菜单选择事件
    handleCommand (command) {
      if (command === 'logout') {
        this.$confirm('确定退出系统吗?', '提示', {
          type: 'warning'
        }).then(() => {
          // this.$ajax.get('/logout').then(() => {
          //   sessionStorage.removeItem('loginUser');
          //   this.$router.push('/login');
          // })
          this.logout();
        })
      } else if (command === 'changepwd') {
        this.dialogVisible = true;
      }
    },
    submitForm () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$message.success('功能研发中...');
          this.dialogVisible = false;
        }
      })
    },
    closeDialog () {
      this.$refs.dataForm.resetFields();
      this.dialogVisible = false;
    },
    logout() {
      sessionStorage.removeItem('loginUser');
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>
  .header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #fff;
  }

  .collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
  }

  .header .logo {
    float: left;
    width: 250px;
    line-height: 70px;
  }

  .header-right {
    float: right;
    padding-right: 50px;
  }

  .header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
  }

  .btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
  }

  .btn-bell,
  .btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
  }

  .btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
  }

  .btn-bell .el-icon-bell {
    color: #fff;
  }

  .user-name {
    margin-left: 10px;
  }

  .user-avator {
    margin-left: 20px;
  }

  .user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }

  .el-dropdown-link {
    color: #fff;
    cursor: pointer;
  }

  .el-dropdown-menu__item {
    text-align: center;
  }

  .sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
  }

  .sidebar::-webkit-scrollbar {
    width: 0;
  }

  .sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
  }

  .sidebar>ul {
    height: 100%;
  }
</style>
